package com.dacool.fragment;

import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.dacool.app.sale.R;
import com.dacool.helper.DaCoolHelper;
import com.dacool.helper.DaCoolHelper.TrackInfoCallback;
import com.dacool.model.BillTrackInfo;
import com.dacool.model.BillTrackInfo.InfoEntry;
import com.dacool.util.ViewUtils;

import static com.dacool.util.ViewUtils.*;

public class BillTrackInfoFragment extends BaseFragment {

	private LinearLayout billtrackInfoContainer;
	private LinearLayout deliverTrackInfoContainer;

	private BillTrackInfo info;

	private long billId;

	private TrackInfoCallback mCallback = new TrackInfoCallback() {

		@Override
		public void didReceiveInfo(boolean success, BillTrackInfo resultInfo) {

			if (!success)
				return;
			info = resultInfo;
			ViewUtils.runInMainThread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub

					if (info == null || info.deliverTracks.size() == 0) {
						deliverTrackInfoContainer.setVisibility(View.GONE);
					}
					billtrackInfoContainer.removeAllViews();
					deliverTrackInfoContainer.removeAllViews();
					LayoutInflater inflater = LayoutInflater
							.from(getActivity());
					LayoutParams lp = new LayoutParams(
							LayoutParams.MATCH_PARENT,
							LayoutParams.WRAP_CONTENT);
					lp.setMargins(rp(30), 0, rp(30), 0);
					ViewHolder vh = ViewHolder.inflate(inflater);
					vh.textView1.setText(String.format("订单编号  %s", info.billID));
					vh.textView2.setText(String.format("订单状态  %s", info.status));
					billtrackInfoContainer.addView(vh.container, lp);

					for (InfoEntry entry : info.billTracks) {
						vh = ViewHolder.inflate(inflater);
						vh.textView1.setText(entry.time);
						vh.textView2.setText(entry.eventDesc);
						billtrackInfoContainer.addView(vh.container, lp);
					}

					for (InfoEntry entry : info.deliverTracks) {
						vh = ViewHolder.inflate(inflater);
						vh.textView1.setText(entry.time);
						vh.textView2.setText(entry.eventDesc);
						deliverTrackInfoContainer.addView(vh.container, lp);
					}
				}
			});

		}
	};

	public void setInfo(BillTrackInfo info) {
		this.info = info;
	}

	public void setBillId(long billId) {
		this.billId = billId;
	}

	@Override
	protected int myLayoutID() {
		// TODO Auto-generated method stub
		return R.layout.frag_trackinfo;
	}

	@Override
	protected View myView() {
		return null;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		billtrackInfoContainer = ViewUtils.findViewById(view,
				R.id.billTrackContainer);
		deliverTrackInfoContainer = ViewUtils.findViewById(view,
				R.id.deliverTrackContainer);

	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		DaCoolHelper.sharedInstance().getBillTrackInfo(billId, mCallback);

	}

	private static class ViewHolder {
		TextView textView1;
		TextView textView2;
		View container;

		static ViewHolder inflate(LayoutInflater inflater) {
			ViewHolder holder = new ViewHolder();
			holder.container = inflater.inflate(R.layout.trackitem, null);
			holder.textView1 = ViewUtils.findViewById(holder.container,
					R.id.text1);
			holder.textView2 = ViewUtils.findViewById(holder.container,
					R.id.text2);
			return holder;
		}
	}

	@Override
	protected boolean showDefaultSecondLeftButton() {
		// TODO Auto-generated method stub
		return true;
	}
}
